$(function(){

function render(){
  $.ajax({
    url:'/cart/queryCart',
    type:'get',
    success:function(info){
      console.log(info);
      if(info.error){
       location.href = 'login.html?backUrl=' + location.href;
      }
      $('.mui-table-view').html( template('tmp',{list:info}) )
      // 结束下拉刷新
      mui('.mui-scroll-wrapper')
      .pullRefresh()
      .endPulldownToRefresh()
    }
  })
}


// 下拉刷新
mui.init({
  pullRefresh : {
    container:".mui-scroll-wrapper",//下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
    down : {
      auto:true,
      callback : function(){
        render()
      }
    }
  }
});


//删除功能    
//mui使用下拉刷新后,click被禁用,用tap代替即可
$('#OA_task_2').on('tap','.btn_delete',function(){
 var id = $(this).data('id');
 mui.confirm('nin确定要删除吗?','温馨提示',['是','否'],function(e){
   if(e.index===0){
     $.ajax({
       url:'/cart/deleteCart',
       type:'get',
       data:{id:id},
       success:function(info){
        console.log(info);
        mui('.mui-scroll-wrapper')
        .pullRefresh()
        .pulldownLoading()
       }
     })
   }
 })
})


//修改功能
$('#OA_task_2').on('tap','.btn_edit',function(){
  var dataObj = this.dataset;


  var html = template('tmp2',dataObj)
  // 转义字符,不懂!!
  html = html.replace(/\n/g,'');

  mui.confirm(html,'编辑商品',['确定','取消'],function(e){
     if(e.index===0){
       var id = dataObj.id;
       var num = $('.mui-numbox-input').val();
       var size = $('.lt_size span.now').text();
       $.ajax({
         url:'/cart/updateCart',
         type:'post',
         data:{
           id:id,
           num:num,
           size:size
         },
         success:function(info){
            if(info.success){
              mui('.mui-scroll-wrapper')
                .pullRefresh()
                .pulldownLoading()
            }
         }
       })
     }
  })


  // 让尺码能选择
  $('.lt_size span').on('click',function(){
    $(this)
    .addClass('now')
    .siblings()
    .removeClass()
  })
  //初始化numbox
  mui('.mui-numbox').numbox()
  
})


//算钱
$('#OA_task_2').on('change','.ck',function(){
  var total=0;
  $checks=$('input:checked');
  $checks.each(function(i,e){
    total += $(this).data('num')*$(this).data('price');
  })
  total=total.toFixed(2);
  $('.total').text(total);
})

})